$().ready(function() {
	validateRule();
});

$.validator.setDefaults({
	submitHandler : function() {
		save();
	}
});
function save() {
	$.ajax({
		cache : true,
		type : "POST",
		url : ctx+"/web/user/save",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);
			} else {
				parent.layer.alert(data.msg)
			}

		}
	});
}

var map = new Map();

//根据用户类型选择组织
function selectDept(){
    var type = $("#type").val();
    $.ajax({
        cache : true,
        type : "GET",
        url : ctx+"/web/user/getDepts?deptType="+type,
        data : {},
        async : false,
        error : function(request) {
            parent.layer.alert("Connection error");
        },
        success : function(data) {
            if(data){
                var html = '';
                if(type!=3){
                    html += '<div class="form-group"><label class="col-sm-3 control-label">所属组织：</label>';
                    html += '<div class="col-sm-8"><select id="deptId" name="deptId" class="form-control">';
                    $(data).each(function(){
                        html += '<option value="'+this.id+'">'+this.name+'</option>';
                    });
                    html += '</select></div></div>';
                }else{
                    html += '<div class="form-group"><label class="col-sm-3 control-label">街道：</label>';
                    html += '<div class="col-sm-8"><select id="streetId" name="streetId" class="form-control"  onchange="selectSq();">';
                    var i = 0;
                    var subHtml = '<div class="form-group"><label class="col-sm-3 control-label">社区：</label>';
                        subHtml += '<div class="col-sm-8"><select id="deptId" name="deptId" class="form-control">';
                    $(data).each(function(){
                        html += '<option value="'+this.id+'">'+this.name+'</option>';
                        map.set(this.id,this.subDepts);
                        if(i == 0){
                            $(this.subDepts).each(function(){
                                var obj = this;
                                subHtml += '<option value="'+obj.id+'">'+obj.name+'</option>';
                            });
                        }
                        i++;
                    });
                    html += '</select></div></div>';
                    subHtml += '</select></div></div>';
                    html+=subHtml;
                }
                $("#deptArea").html(html);
            }else{
                parent.layer.alert("暂无组织信息，请联系管理员")
            }
        }
    });
}

function selectSq(){
    var streetId = $("#streetId").val();
    var subDepts = map.get(parseInt(streetId));
    var subHtml = '';
    $(subDepts).each(function(){
        subHtml += '<option value="'+this.id+'">'+this.name+'</option>';
    });
    $("#deptId").html(subHtml);
}

function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
            mobile : {
				required : true
			},
            cnName : {
                required : true
            },
            password : {
                required : true
            }
		},
		messages : {
            mobile : {
				required : icon + "请输入用户名"
			},
            cnName : {
                required : icon + "请输入姓名"
            },
            password:{
                required:icon + "请输入密码"
            }
		}
	})
}